package com.lzm.listendemo.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lzm.listendemo.R;
import com.lzm.listendemo.bean.WxArticle;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    List<WxArticle> items;
    private onItemClick click = null;
    private Context context;
    public MyAdapter(Activity context, List<WxArticle> items) {
        this.items = items;
        this.context = context;
    }

    @NonNull
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.ViewHolder holder, int position) {
        WxArticle item = items.get(position);
        holder.text.setText(item.getTitle());
        if(click!=null){
            holder.itemView.setOnClickListener(v->click.onClick(v,item));
        }
    }

    public interface onItemClick{
        void onClick(View view,WxArticle wxArticle);
    }


    @Override
    public int getItemCount() {
        return items.size();
    }

    public void setList(List<WxArticle> wxArticles) {
        this.items = wxArticles;
        notifyDataSetChanged();
    }

    public void setItemOnClick(onItemClick onClick) {
        this.click = onClick;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView text;

        public ViewHolder(View view) {
            super(view);
            text = (TextView) view.findViewById(R.id.text);
        }

    }

}